package com.xx.pmis.web.actions;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Present;
import com.xx.pmis.model.Shop;
import com.xx.pmis.model.Wine;
import com.xx.pmis.services.PresentService;
import com.xx.pmis.services.ShopService;
import com.xx.pmis.services.WineService;
import com.xx.pmis.web.forms.WineForm;
import com.xx.pmis.web.util.AccountUtil;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.Rand;
import com.xx.pmis.web.util.WineUtil;

/**
 * @class 葡萄酒Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class WineAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(WineAction.class);

	ShopService shopService = getPMISServices().getShopService();
	WineService wineService = getPMISServices().getWineService();
	PresentService presentService = getPMISServices().getPresentService();

	AccountUtil accountUtil = new AccountUtil();
	WineUtil wineUtil = new WineUtil();

	// 查询所有葡萄酒
	public ActionForward listWine(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<Wine> wineList = null;
		try {
			wineList = wineService.getAllWineByCondition(" order by wineDate desc");
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒】查询【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("wineList", wineList);
		request.setAttribute("position", "物资管理/葡萄酒");
		return mapping.findForward("listWine");
	}

	// 新增
	public ActionForward addWine(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> shopList = null;
		try {
			shopList = shopService.getAllShopByShopType("1");
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/新增】查询【商店】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("shopList", shopList);
		request.setAttribute("wineDate", CurrentDate.currentDate());
		request.setAttribute("position", "葡萄酒/新增");
		return mapping.findForward("addWine");
	}

	// 新增，处理方法
	public ActionForward addWine_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		WineForm f = (WineForm) form;

		String wineDate = f.getWineDate(); // 购买日期
		String wineName = f.getWineName(); // 葡萄酒名称
		String wineType = f.getWineType(); // 类型
		String wineCountry = f.getWineCountry(); // 产地
		String wineLevel = f.getWineLevel(); // 等级
		String wineAlcohol = f.getWineAlcohol(); // 葡萄酒精度
		String winePrice = f.getWinePrice(); // 单价
		String wineComment = f.getWineComment(); // 评论，指专业的品酒师评论
		String wineNum = f.getWineNum(); // 数量
		String wineFlavour = f.getWineFlavour();// 香味
		String wineColour = f.getWineColour();// 色泽
		String grapeVariety = f.getGrapeVariety();// 葡萄品种
		String specifications = f.getSpecifications(); // 规格
		String shopID = f.getShopID();// 商店ID
		String boxRegulation = f.getBoxRegulation(); // 箱规
		String productionDate = f.getProductionDate(); // 生产日期

		// 优惠
		String wine1 = f.getWine1();
		String wine2 = f.getWine2();
		String wine3 = f.getWine3();
		String wineVariety = f.getWineVariety();
		// String wineState = f.getWineState();

		Wine wine = new Wine();
		wine.setWineID(CurrentDate.currentYear() + Rand.getRand());
		wine.setWineDate(wineDate);
		wine.setWineName(wineName);
		wine.setWineType(wineType);
		wine.setWineCountry(wineCountry);
		wine.setWineLevel(wineLevel);

		wine.setWineAlcohol(wineAlcohol);
		wine.setWinePrice(accountUtil.if1ZeroTo2Zero(winePrice));
		wine.setWineState("1");
		// 20131112 不通过本模块支付，根据实际购买需要到商品里支付
		wine.setWinePay("0");
		wine.setWineComment(wineComment);
		wine.setWineNum(wineNum);
		wine.setWineFlavour(wineFlavour);
		wine.setWineColour(wineColour);
		wine.setGrapeVariety(grapeVariety);
		;
		wine.setSpecifications(specifications);
		wine.setRepertoryNum(wineNum);
		wine.setBoxRegulation(boxRegulation);
		wine.setWine1(wine1);
		wine.setWine2(wine2);
		wine.setWine3(wine3);
		wine.setWineVariety(wineVariety);
		wine.setShopID(shopID);
		wine.setProductionDate(productionDate);

		try {
			wineService.saveOrUpdateWine(wine);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/新增】保存或更新【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		// 商品
		Present present = new Present();
		present.setPresentID(CurrentDate.currentYear() + Rand.getRand());
		present.setPresentName(wineName);
		present.setShopID(shopID);
		present.setPresentDate(wineDate);
		present.setPresentType("1");
		present.setPresentPay("1");
		// 请根据实际情况修改
		present.setPresentExpenses("0.00");
		present.setPresentState("0");
		present.setPresentDesc(wineName);

		try {
			presentService.saveOrUpdatePresent(present);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/新增】保存或更新【商品】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listWineByPage(mapping, form, request, response);
		return mapping.findForward("listWine");
	}

	// 编辑
	public ActionForward editWine(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String wineID = request.getParameter("wineID");
		List<Shop> shopList = null;
		try {
			shopList = shopService.getAllShop();
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/编辑】查询【商店】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("shopList", shopList);
		Wine wine = null;
		try {
			wine = wineService.getWineByWineID(wineID);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/编辑】根据ID查询该【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		Shop shop = null;
		try {
			shop = shopService.getShopByShopID(wine.getShopID());
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/编辑】根据ID查询该【商店】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("wine", wine);
		request.setAttribute("shopName", shop.getShopName());
		request.setAttribute("position", "葡萄酒/编辑");
		return mapping.findForward("editWine");
	}

	// 编辑，处理方法
	public ActionForward editWine_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");
		log.info("------------------------begin-------------------------------");
		log.info("pageNum:" + pageNum);
		log.info("------------------------end---------------------------------");
		WineForm f = (WineForm) form;
		String wineID = f.getWineID();
		String wineDate = f.getWineDate(); // 购买日期
		String wineName = f.getWineName(); // 葡萄酒名称
		String wineType = f.getWineType(); // 类型
		String wineCountry = f.getWineCountry(); // 产地
		String wineLevel = f.getWineLevel(); // 等级
		String wineAlcohol = f.getWineAlcohol(); // 葡萄酒精度
		String winePrice = f.getWinePrice(); // 购买件
		String wineComment = f.getWineComment(); // 评论，指专业的品酒师评论
		String wineNum = f.getWineNum(); // 数量
		String wineFlavour = f.getWineFlavour();// 香味
		String wineColour = f.getWineColour();// 色泽
		String grapeVariety = f.getGrapeVariety();// 葡萄品种
		String specifications = f.getSpecifications(); // 规格
		String shopID = f.getShopID();// 商店ID
		String repertoryNum = f.getRepertoryNum();// 库存量
		String boxRegulation = f.getBoxRegulation(); // 箱规
		String productionDate = f.getProductionDate(); // 生产日期

		// 优惠
		String wine1 = f.getWine1();
		String wine2 = f.getWine2();
		String wine3 = f.getWine3();
		String wineVariety = f.getWineVariety();

		Wine wine = null;
		try {
			wine = wineService.getWineByWineID(wineID);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/编辑】根据【葡萄酒ID】查询该【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		wine.setWineAlcohol(wineAlcohol);
		wine.setWineDate(wineDate);
		wine.setWineName(wineName);
		wine.setWineType(wineType);
		wine.setWineCountry(wineCountry);
		wine.setWineLevel(wineLevel);
		wine.setWinePrice(accountUtil.if1ZeroTo2Zero(winePrice));
		wine.setWineComment(wineComment);
		wine.setWineNum(wineNum);
		wine.setWineFlavour(wineFlavour);
		wine.setWineColour(wineColour);
		wine.setGrapeVariety(grapeVariety);
		wine.setSpecifications(specifications);
		wine.setRepertoryNum(repertoryNum);
		wine.setBoxRegulation(boxRegulation);
		wine.setWine1(wine1);
		wine.setWine2(wine2);
		wine.setWine3(wine3);
		wine.setWineVariety(wineVariety);
		wine.setShopID(shopID);
		wine.setProductionDate(productionDate);
		try {
			wineService.saveOrUpdateWine(wine);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/编辑】保存或更新【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		listWineByPage(mapping, form, request, response);
		return mapping.findForward("listWine");
	}

	// 删除
	public ActionForward delWine(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");
		log.info("------------------------begin-------------------------------");
		log.info("pageNum:" + pageNum);
		log.info("------------------------end---------------------------------");
		Wine wine = null;
		WineForm f = (WineForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				wine = wineService.getWineByWineID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【物资管理/葡萄酒/删除】根据【葡萄酒ID】查询该【葡萄酒】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				wineService.deleteWine(wine);
			} catch (Exception e) {
				String errorPage = "【物资管理/葡萄酒/删除】删除【葡萄酒】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		request.setAttribute("pageNum", pageNum);
		listWineByPage(mapping, form, request, response);
		return mapping.findForward("listWine");
	}

	// 明细
	public ActionForward detailWine(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String wineID = request.getParameter("wineID");

		Wine wine = new Wine();
		try {
			wine = wineService.getWineByWineID(wineID);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/编辑】根据【葡萄酒ID】查询该【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("wine", wine);
		request.setAttribute("position", "葡萄酒/明细");
		return mapping.findForward("detailWine");
	}

	// 结算
	public ActionForward payWine(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String wineID = request.getParameter("wineID");
		Wine wine = new Wine();
		try {
			wine = wineService.getWineByWineID(wineID);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/结算】根据【葡萄酒ID】查询该【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		// 生成记账凭证
		accountUtil.payExpenseToAccount(wine.getWineDate(), "0030010", wine.getRepertoryNum(), wine.getWinePrice(),
				"购买", wine.getWineName());
		// 更新结算状态，结算完成为"0"
		wine.setWinePay("0");
		try {
			wineService.saveOrUpdateWine(wine);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/结算】保存或更新【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listWineByPage(mapping, form, request, response);
		return mapping.findForward("listWine");
	}

	// 分页显示
	public ActionForward listWineByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = wineService.getCountOfWineByCondition("");
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒】查询【葡萄酒】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}
		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<Wine> wineList = null;
		// 在前台显示商店名称
		List<Wine> wineListTemp = new ArrayList<Wine>();
		try {
			wineList = wineService.getAllWineByConditionByPage(" order by wineDate desc", Integer.parseInt(pageNum),
					PAGESIZE);
			for (Wine wine : wineList) {
				Shop shop = new Shop();
				try {
					shop = shopService.getShopByShopID(wine.getShopID());
				} catch (Exception e) {
					String errorPage = "【物资管理/葡萄酒/分页显示】根据商店ID查询【商店】异常！" + e.toString();
					log.info(errorPage);
				}
				wine.setShopName(shop.getShopName());
				// 当前年份
				String currentYear = CurrentDate.currentYear();
				// 生产年份
				String productionYear = wine.getProductionDate().substring(0, 4);
				// 储存年数=当前年份-生产年份
				wine.setWineYear(String.valueOf(Integer.parseInt(currentYear) - Integer.parseInt(productionYear)));
				wineListTemp.add(wine);
			}
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/分页显示】查询【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("wineList", wineListTemp);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "wine.do");
		request.setAttribute("position", "物资管理/葡萄酒");
		return mapping.findForward("listWine");
	}

	// 分页显示（货款平衡）
	public ActionForward listWineAttachByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = wineService.getCountOfWineByCondition(" where wineState = 1");
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒】查询【葡萄酒】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}
		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<?> wineAttachList = null;
		try {
			wineAttachList = wineUtil.getAllWineAttachByPage(Integer.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒】查询【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("wineAttachList", wineAttachList);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "wine.do");
		request.setAttribute("position", "白酒/葡萄酒/货款平衡");
		return mapping.findForward("listWineAttach");
	}

	// 更新状态 0为缺货，1为存货
	public ActionForward updateWineState(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String wineID = request.getParameter("wineID");
		String state = request.getParameter("state");
		String pageNum = request.getParameter("pageNum");

		Wine wine = new Wine();
		try {
			wine = wineService.getWineByWineID(wineID);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/更新状态】根据【葡萄酒ID】查询该【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		if ("quehuo".equals(state)) {// 缺货
			wine.setWineState("1");
		} else if ("cunhuo".equals(state)) {// 存货
			wine.setWineState("2");
			// 把库存量设置为0
			wine.setRepertoryNum("0");
		} else {
			log.error("输入参数应为“quehuo”，“cunhuo”。");
		}

		try {
			wineService.saveOrUpdateWine(wine);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/编辑】保存或更新【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		listWineByPage(mapping, form, request, response);
		return mapping.findForward("listWine");
	}

	// 复制
	public ActionForward copyWine(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String wineID = request.getParameter("wineID");
		log.info("" + wineID);
		Wine wine = null;
		try {
			wine = wineService.getWineByWineID(wineID);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/编辑】根据【葡萄酒ID】查询该【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		Wine wineTemp = new Wine();
		wineTemp.setWineDate(CurrentDate.currentDate());
		wineTemp.setWineName(wine.getWineName());
		wineTemp.setWineType(wine.getWineType());
		wineTemp.setWineCountry(wine.getWineCountry());
		wineTemp.setWineLevel(wine.getWineLevel());
		wineTemp.setWineYear(wine.getWineYear());
		wineTemp.setWineAlcohol(wine.getWineAlcohol());
		wineTemp.setWinePrice(accountUtil.if1ZeroTo2Zero(wine.getWinePrice()));
		wineTemp.setWineState("1");
		// 20131112 不通过本模块支付，根据实际购买需要到商品里支付
		wineTemp.setWinePay("0");
		wineTemp.setWineComment(wine.getWineComment());
		wineTemp.setWineNum("0");
		wineTemp.setWineFlavour(wine.getWineFlavour());
		wineTemp.setWineColour(wine.getWineColour());
		wineTemp.setGrapeVariety(wine.getGrapeVariety());
		wineTemp.setSpecifications(wine.getSpecifications());
		wineTemp.setTemperature(wine.getTemperature());
		wineTemp.setRepertoryNum("0");
		wineTemp.setBoxRegulation(wine.getBoxRegulation());
		wineTemp.setWine1(wine.getWine1());
		wineTemp.setWine2(wine.getWine2());
		wineTemp.setWine3(wine.getWine3());
		wineTemp.setShopID(wine.getShopID());
		wineTemp.setProductionDate(wine.getProductionDate());

		try {
			wineService.saveOrUpdateWine(wineTemp);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/新增】保存或更新【葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		// 商品
		Present present = new Present();
		present.setPresentName(wineTemp.getWineName());
		present.setShopID(wineTemp.getShopID());
		present.setPresentDate(wineTemp.getWineDate());
		present.setPresentType("1");
		present.setPresentPay("1");
		// 请根据实际情况修改
		present.setPresentExpenses("0.00");
		present.setPresentState("0");
		present.setPresentDesc(wineTemp.getWineName());

		try {
			presentService.saveOrUpdatePresent(present);
		} catch (Exception e) {
			String errorPage = "【物资管理/葡萄酒/新增】保存或更新【商品】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listWineByPage(mapping, form, request, response);
		return mapping.findForward("listWine");
	}

	// 查询
	public ActionForward queryWine(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("position", "商品/查询");
		request.setAttribute("wineState0", "checked=\"checked\"");
		request.setAttribute("wineType0", "checked=\"checked\"");
		request.setAttribute("wineVariety0", "checked=\"checked\"");
		return mapping.findForward("queryWine");
	}

	// 查询
	public ActionForward queryWine_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		WineForm f = (WineForm) form;
		String wineName = f.getWineName(); // 商品名称
		String wineState = f.getWineState();
		String wineType = f.getWineType();
		String wineVariety = f.getWineVariety();
		log.info("wineState******************************************" + wineState);
		log.info("wineType******************************************" + wineType);
		log.info("wineVariety******************************************" + wineVariety);

		// 库存状态
		if (wineState.equals("0")) {
			request.setAttribute("wineState0", "checked=\"checked\"");
			request.setAttribute("wineState1", "");
			request.setAttribute("wineState2", "");
		} else if (wineState.equals("1")) {
			request.setAttribute("wineState0", "");
			request.setAttribute("wineState1", "checked=\"checked\"");
			request.setAttribute("wineState2", "");
		} else if (wineState.equals("2")) {
			request.setAttribute("wineState0", "");
			request.setAttribute("wineState1", "");
			request.setAttribute("wineState2", "checked=\"checked\"");
		} else {
			log.error("错误的wineSate:" + wineState + "值!");
		}

		// 商品类型
		if (wineType.equals("0")) {
			request.setAttribute("wineType0", "checked=\"checked\"");
			request.setAttribute("wineType1", "");
			request.setAttribute("wineType2", "");
			request.setAttribute("wineType3", "");
			request.setAttribute("wineType9", "");
		} else if (wineType.equals("1")) {
			request.setAttribute("wineType0", "");
			request.setAttribute("wineType1", "checked=\"checked\"");
			request.setAttribute("wineType2", "");
			request.setAttribute("wineType3", "");
			request.setAttribute("wineType9", "");
		} else if (wineType.equals("2")) {
			request.setAttribute("wineType0", "");
			request.setAttribute("wineType1", "");
			request.setAttribute("wineType2", "checked=\"checked\"");
			request.setAttribute("wineType3", "");
			request.setAttribute("wineType9", "");
		} else if (wineType.equals("3")) {
			request.setAttribute("wineType0", "");
			request.setAttribute("wineType1", "");
			request.setAttribute("wineType2", "");
			request.setAttribute("wineType3", "checked=\"checked\"");
			request.setAttribute("wineType9", "");
		} else if (wineType.equals("9")) {
			request.setAttribute("wineType0", "");
			request.setAttribute("wineType1", "");
			request.setAttribute("wineType2", "");
			request.setAttribute("wineType3", "");
			request.setAttribute("wineType9", "checked=\"checked\"");
		} else {
			log.error("错误的wineType:" + wineType + "值!");
		}

		// 用途
		if (wineVariety.equals("0")) {
			request.setAttribute("wineVariety0", "checked=\"checked\"");
			request.setAttribute("wineVariety1", "");
			request.setAttribute("wineVariety2", "");
			request.setAttribute("wineVariety3", "");
		} else if (wineVariety.equals("1")) {
			request.setAttribute("wineVariety0", "");
			request.setAttribute("wineVariety1", "checked=\"checked\"");
			request.setAttribute("wineVariety2", "");
			request.setAttribute("wineVariety3", "");
		} else if (wineVariety.equals("2")) {
			request.setAttribute("wineVariety0", "");
			request.setAttribute("wineVariety1", "");
			request.setAttribute("wineVariety2", "checked=\"checked\"");
			request.setAttribute("wineVariety3", "");
		} else if (wineVariety.equals("3")) {
			request.setAttribute("wineVariety0", "");
			request.setAttribute("wineVariety1", "");
			request.setAttribute("wineVariety2", "");
			request.setAttribute("wineVariety3", "checked=\"checked\"");
		} else {
			log.error("错误的wineSate:" + wineState + "值!");
		}

		StringBuffer hql = new StringBuffer();

		if (!"".equals(wineName)) {
			hql.append(" and wineName like '%" + wineName + "%'");
		}
		if (!wineState.equals("0")) {
			hql.append(" and wineState = '" + wineState + "'");
		}
		if (!wineType.equals("0")) {
			hql.append(" and wineType = '" + wineType + "'");
		}
		if (!wineVariety.equals("0")) {
			hql.append(" and wineVariety = '" + wineVariety + "'");
		}

		String condition = hql.toString();
		if (!condition.equals("") || condition != "") {
			if (condition.startsWith(" and")) {
				condition = condition.substring(4);
				condition = " where" + condition;
			}
		}

		log.info("condition******************************************" + condition);

		// 查询总用户记录数
		int count = 0;
		try {
			count = wineService.getCountOfWineByCondition(condition);
		} catch (Exception e) {
			String errorPage = "【物资管理/白酒/葡萄酒】查询【白酒/葡萄酒】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("count", "共检索到<B><font color=\"red\"> " + count + " </font></B>条记录");

		List<Wine> wineList = null;
		// 在前台显示商店名称
		List<Wine> wineListTemp = new ArrayList<Wine>();
		try {
			wineList = wineService.getAllWineByCondition(condition + " order by wineDate desc");
			for (Wine wine : wineList) {
				Shop shop = new Shop();
				try {
					shop = shopService.getShopByShopID(wine.getShopID());
				} catch (Exception e) {
					String errorPage = "【物资管理/葡萄酒/分页显示】根据商店ID查询【商店】异常！" + e.toString();
					log.info(errorPage);
				}
				wine.setShopName(shop.getShopName());
				// 当前年份
				String currentYear = CurrentDate.currentYear();
				// 生产年份
				String productionYear = wine.getProductionDate().substring(0, 4);
				// 储存年数=当前年份-生产年份
				wine.setWineYear(String.valueOf(Integer.parseInt(currentYear) - Integer.parseInt(productionYear)));
				wineListTemp.add(wine);
			}
		} catch (Exception e) {
			String errorPage = "【物资管理/白酒/葡萄酒】查询【白酒/葡萄酒】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("wineList", wineListTemp);
		request.setAttribute("wineName", wineName);
		request.setAttribute("wineState", wineState);
		request.setAttribute("position", "商品/查询");
		return mapping.findForward("queryWine");
	}

	public static void main(String arg[]) {
		StringBuffer hql = new StringBuffer();
		String wineState = "99";
		if (wineState == "99") {
			hql.append(" and wineState = '" + wineState + "'");
		}
		System.out.println("*******" + hql.toString());
	}
}